package com.gxa.service.impl;

import com.gxa.pojo.*;
import com.gxa.service.PurchaseService;
import com.gxa.mapper.PurchaseMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author Tian同學
 * @Date 2020/9/21 17:37
 * @Version 1.0
 */
@Service(value = "purchaseService")
public class PurchaseServiceImpl implements PurchaseService {

    @Resource
    PurchaseMapper purchaseMapper;

    /**
     * 采购管理-分页查询
     * @param start
     * @param pageSize
     * @return
     */
    @Override
    @Transactional(isolation = Isolation.DEFAULT,propagation = Propagation.SUPPORTS,rollbackFor = {Throwable.class})
    public List<Purchase> findAllPurchaseByPage(Integer start, Integer pageSize) {
        return purchaseMapper.findAllPurchaseByPage(start,pageSize);
    }

    /**
     * 采购管理-分页查询总条数
     * @return
     */
    @Override
    @Transactional(isolation = Isolation.DEFAULT,propagation = Propagation.SUPPORTS,rollbackFor = {Throwable.class})
    public Integer findAllPurchase() {
        return purchaseMapper.findAllPurchase();
    }

    /**
     * 采购管理-查询供货厂商-下拉框
     * @return
     */
    @Override
    public List<Purchase> selectAllPurchase() {
        return purchaseMapper.selectAllPurchase();
    }

    /**
     * 采购管理-筛选查询 创建日期-供货厂商-采购状态 模糊查询
     * @param map
     * @return
     */
    @Override
    @Transactional(isolation = Isolation.DEFAULT,propagation = Propagation.SUPPORTS,rollbackFor = {Throwable.class})
    public List<Map<String,Object>> selectByLike(Map map) {
        String name = (String) map.get("name");
        String allUserNameManager = purchaseMapper.findAllUserNameManager(name);
        if(allUserNameManager.equals("superadmin") || allUserNameManager.equals("admin") || allUserNameManager.equals("检查员")){
            map.put("name",null);

        }
        return purchaseMapper.selectByLike(map);
    }

    /**
     * 采购管理-筛选查询 创建日期-供货厂商-采购状态 模糊查询总条数
     * @param map
     * @return
     */
    @Override
    @Transactional(isolation = Isolation.DEFAULT,propagation = Propagation.SUPPORTS,rollbackFor = {Throwable.class})
    public Integer selectByLikeCount(Map map) {
        return purchaseMapper.selectByLikeCount(map);
    }

    /**
     * 采购管理-预览商品明细
     * @param id
     * @return
     */
    @Override
    @Transactional(isolation = Isolation.DEFAULT,propagation = Propagation.SUPPORTS,rollbackFor = {Throwable.class})
    public List<Purchase> findAllPurchaseByDetailed(Integer id) {
        return purchaseMapper.findAllPurchaseByDetailed(id);
    }

    /**
     * 采购管理-修改数据-编辑
     * @param map
     * @return
     */
    @Override
    @Transactional(isolation = Isolation.DEFAULT,propagation = Propagation.REQUIRED,rollbackFor = {Throwable.class})
    public int updateByIdPurchase(Map map) {
        return purchaseMapper.updateByIdPurchase(map);
    }

    /**
     * 采购申请-插入新数据 添加按钮
     * @param map
     * @return
     */
    @Override
    @Transactional(isolation = Isolation.DEFAULT,propagation = Propagation.REQUIRED,rollbackFor = {Throwable.class})
    public int insertByPurchase(Map map) {
        return purchaseMapper.insertByPurchase(map);
    }

    /**
     * 采购申请-获取新添加数据的ID(最大ID)
     * @return
     */
    @Override
    public int selectPurchaseId() {
        return purchaseMapper.selectPurchaseId();
    }

    /**
     * 采购申请-根据id删除
     * @param id
     * @return
     */
    @Override
    @Transactional(isolation = Isolation.DEFAULT,propagation = Propagation.REQUIRED,rollbackFor = {Throwable.class})
    public int deleteByPurchase(Integer id) {

        return purchaseMapper.deleteByPurchase(id);
    }

    /**
     * 采购管理-插入审批意见+状态
     * @param map
     * @return
     */
    @Override
    @Transactional(isolation = Isolation.DEFAULT,propagation = Propagation.REQUIRED,rollbackFor = {Throwable.class})
    public int insertByApproval(Map map) {
        return purchaseMapper.insertByApproval(map);
    }

    @Override
    @Transactional(isolation = Isolation.DEFAULT,propagation = Propagation.REQUIRED,rollbackFor = {Throwable.class})
    public int insertByApproval2(Map map) {
        int i = purchaseMapper.insertByApproval(map);
        String id = (String) map.get("id");
        List<Map> commoditydetailsNameById = purchaseMapper.findCommoditydetailsNameById(id);

        for (Map map1 : commoditydetailsNameById) {
            String tradeName = (String) map1.get("tradeName");
            GoodsStorage goodsStorageByName = purchaseMapper.findGoodsStorageByName(tradeName);
            if(goodsStorageByName!=null){
                purchaseMapper.updateGoodsStorageStock((String) map1.get("quantityLarge"),
                        (String) map1.get("quantitySnall"),goodsStorageByName.getId());
            }else {
                Merchandise merchandiseByName = purchaseMapper.findMerchandiseByName(tradeName);

                String brand = merchandiseByName.getBrand();
                String type = merchandiseByName.getType();
                map1.put("brand",brand);
                map1.put("classification",type);
                purchaseMapper.insertGoodsStorage(map1);
            }
        }
        return i;
    }



    /**
     * 采购申请-查询供货厂商-下拉框
     * @return
     */
    @Override
    @Transactional(isolation = Isolation.DEFAULT,propagation = Propagation.SUPPORTS,rollbackFor = {Throwable.class})
    public List<Tb_Goodsstorage> selectByTb_GoodsstorageSupplier() {
        return purchaseMapper.selectByTb_GoodsstorageSupplier();
    }

    /**
     * 采购申请-查询审批人-下拉框
     * @return
     */
    @Override
    public List<User> selectByApproved() {
        return purchaseMapper.selectByApproved();
    }

    /**
     * 采购申请-查询商品品牌-选择品牌下拉框
     * @return
     */
    @Override
    @Transactional(isolation = Isolation.DEFAULT,propagation = Propagation.SUPPORTS,rollbackFor = {Throwable.class})
    public List<Tb_Goodsstorage> selectByTb_GoodsstorageBrand() {
        return purchaseMapper.selectByTb_GoodsstorageBrand();
    }

    /**
     * 采购申请-选择商品分类-选择分类下拉框
     * @return
     */
    @Override
    @Transactional(isolation = Isolation.DEFAULT,propagation = Propagation.SUPPORTS,rollbackFor = {Throwable.class})
    public List<Tb_Goodsstorage> selectByTb_GoodsstorageBrandClassification() {
        return purchaseMapper.selectByTb_GoodsstorageBrandClassification();
    }

    /**
     * 采购申请 选择品牌-选择分类-商品标题 模糊查询
     * @param map
     * @return
     */
    @Override
    @Transactional(isolation = Isolation.DEFAULT,propagation = Propagation.SUPPORTS,rollbackFor = {Throwable.class})
    public List<Map<String,Object>> selectByTb_GoodsstorageLike(Map map) {
        return purchaseMapper.selectByTb_GoodsstorageLike(map);
    }

    /**
     *  采购申请 选择品牌-选择分类-商品标题 模糊查询总条数
     * @param map
     * @return
     */
    @Override
    @Transactional(isolation = Isolation.DEFAULT,propagation = Propagation.SUPPORTS,rollbackFor = {Throwable.class})
    public Integer selectByTb_GoodsstorageLikeCount(Map map) {
        return purchaseMapper.selectByTb_GoodsstorageLikeCount(map);
    }





}
